package com.shj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shj.model.*;
import com.shj.mapper.SysConclusionDao;
import com.shj.util.JsonUtil;
import com.shj.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * (SysConclusion)表服务实现类
 *
 * @author makejava
 * @since 2020-03-12 12:42:44
 */
@Service("sysConclusionService")
public class SysConclusionService {
    @Resource
    private SysConclusionDao sysConclusionDao;
    @Autowired
    private SysSubFreeService sysSubFreeService;
    @Autowired
    private SysSubjectService sysSubjectService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public SysConclusion queryById(Integer id) {
        SysConclusion sysConclusion = sysConclusionDao.queryById(id);
        SysSubFree sysSubFree = sysSubFreeService.queryBySubId(sysConclusion.getSubId(), 1);
        sysConclusion.setFree(sysSubFree);
        return sysConclusion;
    }

    /**
     * @Description 管理员分页查询数据
     * @author songhuajian
     * @date 2020/3/19 15:00
     */
    public PageResult findPage(PageRequest pageRequest, String leaderId){
        List<SysConclusion> list = sysConclusionDao.queryAll(leaderId);
        return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest, list));
    }


    /**
     * @Description 调用分页插件完成分页
     * @author songhuajian
     * @date 2020/3/19 14:38
     */
    public PageInfo<SysConclusion> getPageInfo(PageRequest pageRequest, List<SysConclusion> list){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<SysConclusion>(list);
    }

    /**
     * 新增数据
     *
     * @param sysConclusion 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public SysConclusion insert(SysConclusion sysConclusion) throws IOException {
        // 添加预算费用
        String freeJson = (String) sysConclusion.getFree();
        SysSubFree sysSubFree = (SysSubFree) JsonUtil.jsonToObj(new SysSubFree(), freeJson);
        sysSubFree.setSubId(sysConclusion.getSubId());
        sysSubFree.setFreeType(1);
        sysSubFreeService.insert(sysSubFree);
        // 添加结题
        this.sysConclusionDao.insert(sysConclusion);
        // 修改项目状态
        sysSubjectService.updateStatus(sysConclusion.getSubId());
        return sysConclusion;
    }

    /**
     * 修改数据
     *
     * @param sysConclusion 实例对象
     * @return 实例对象
     */
    public SysConclusion update(SysConclusion sysConclusion) {
        this.sysConclusionDao.update(sysConclusion);
        return this.queryById(sysConclusion.getId());
    }

    public SysConclusion review(SysConclusion sysConclusion) {
        this.sysConclusionDao.review(sysConclusion);
        return this.queryById(sysConclusion.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id) {
        return this.sysConclusionDao.deleteById(id) > 0;
    }


    public ConclusionCensus countConclusion() {
        int countAll = sysConclusionDao.countAll();
        int countNational = sysConclusionDao.countNational();
        int countDistrict = sysConclusionDao.countDistrict();
        int countGrand = sysConclusionDao.countGrand();
        int countFirst = sysConclusionDao.countFirst();
        int countSecond = sysConclusionDao.countSecond();
        int countThird = sysConclusionDao.countThird();
        int countExcellence = sysConclusionDao.countExcellence();
        int countNothing = sysConclusionDao.countNothing();

        ConclusionCensus conclusionCensus = new ConclusionCensus();
        conclusionCensus.setCount(countAll);
        conclusionCensus.setNationalLevel(countNational);
        conclusionCensus.setDistrictLevel(countDistrict);
        conclusionCensus.setGrandPrize(countGrand);
        conclusionCensus.setFirstPrize(countFirst);
        conclusionCensus.setSecondPrize(countSecond);
        conclusionCensus.setThirdPrize(countThird);
        conclusionCensus.setExcellencePrize(countExcellence);
        conclusionCensus.setNothing(countNothing);
        return conclusionCensus;
    }
}